﻿using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Faust.Andrew.MTG.Player.Shared
{
    [DataContract]
    public class Counter : INotifyPropertyChanged, ICloneable
    {
        public Counter()
        {
            _color = System.Windows.Media.Colors.Transparent;
        }

        private string _name;
        /// <summary>
        /// The Name of the Counter. Examples (Power, Toughness, Sprore, Storage, etc)
        /// </summary>
        [DataMember]
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    FirePropertyChanged("Name");
                }
            }
        }

        private CounterType _counterType;
        [DataMember]
        public CounterType CounterType
        {
            get
            {
                return _counterType;
            }
            set
            {
                if (_counterType != value)
                {
                    _counterType = value;
                    FirePropertyChanged("CounterType");
                }
            }
        }

        private System.Windows.Media.Color _color;
        [DataMember]
        public System.Windows.Media.Color Color
        {
            get { return _color; }
            set
            {
                if (_color != value)
                {
                    _color = value;
                    FirePropertyChanged("Color");
                }
            }
        }

        private int _value;
        /// <summary>
        /// Value of the counter can be positive or negative
        /// </summary>
        [DataMember]
        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (_value != value)
                {
                    _value = value;
                    FirePropertyChanged("Value");
                }
            }
        }

        private string _stringValue;

        [DataMember]
        public string StringValue
        {
            get
            {
                if (String.IsNullOrEmpty(_stringValue) && _value != 0)
                {
                    return _value.ToString();
                }
                else
                {
                    return _stringValue;
                }
            }
            set
            {
                if (_stringValue != value)
                {
                    _stringValue = value;
                    FirePropertyChanged("StringValue");
                }
            }
        }
        

        public override string ToString()
        {
            return Name + " - " + Value;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region ICloneable Members

        public object Clone()
        {
            return new Counter()
            {
                Name = Name,
                CounterType = CounterType,
                Value = Value,
                Color = Color
            };
        }

        #endregion
    }
}
